package com.flyaudio.xiaopengautopilot.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.DataSet;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.listener.ChartTouchListener;
import com.github.mikephil.charting.listener.OnChartGestureListener;
import com.github.mikephil.charting.listener.OnDrawListener;
import com.github.mikephil.charting.utils.MPPointF;

import java.util.ArrayList;

public class FlyBarChart extends BarChart {

    private final String TAG = "FlyBarChart";

    private float pxForOneX = 0;  //每个Entry占用的px值
    private int dataCountInPage = 31; //显示的条形图数
    private float pxBar = 14f/2; //条形图宽的1/2
    private float firstXPos = 40f + pxBar;  //第一个条形图中点
    private ChartTouchListener OnTouchListener;

    public void setOnCharItemIndex(OnCharItemIndex onCharItemIndex) {
        this.onCharItemIndex = onCharItemIndex;
    }

    private OnCharItemIndex onCharItemIndex = null;
    public interface OnCharItemIndex {
        void onItemIndex(int index);
    }
    public FlyBarChart(Context context) {
        super(context);
        Log.d(TAG, "1 " );
    }

    public FlyBarChart(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.d(TAG, "2 ");
    }

    public FlyBarChart(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        Log.d(TAG, "3 ");
    }
    public void clear(){
        pxForOneX = 0;

    }
    public void initPxForOneX(int count,float pxBar)
    {
        dataCountInPage = count;
        this.pxBar = pxBar;
        if (pxForOneX == 0) {
            IBarDataSet list = getBarData().getDataSetByIndex(0);
            if (list.getEntryCount() > 1) {
                float first = getPosition(list.getEntryForIndex(0), YAxis.AxisDependency.RIGHT).getX();
                float lastXPos = getPosition(list.getEntryForIndex(list.getEntryCount() - 1), YAxis.AxisDependency.RIGHT).getX();
                pxForOneX = (lastXPos - first) / (list.getEntryCount() - 1);
                Log.d(TAG, "firstXPos " + firstXPos + " pxForOneX " + pxForOneX);
            }
        }
    }
    private void computeIndex()
    {
        if (getBarData().getDataSetCount() > 0) {
            IBarDataSet list = getBarData().getDataSetByIndex(0);
            if (list.getEntryCount() > 0) {
                MPPointF first = getPosition(list.getEntryForIndex(0), YAxis.AxisDependency.RIGHT);
                int index = 0;
                if (firstXPos > first.getX()) {
                    index = (int) ((firstXPos - first.getX()) / pxForOneX);
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                else
                {
                    getParent().requestDisallowInterceptTouchEvent(false);
                }
                Log.d(TAG,"first.getX() " + first.getX());
                if (index < list.getEntryCount() - dataCountInPage) {
                    MPPointF cur = getPosition(list.getEntryForIndex(index), YAxis.AxisDependency.RIGHT);
                    if (firstXPos - cur.getX() > pxBar)
                        index += 1;
                    cur = getPosition(list.getEntryForIndex(index), YAxis.AxisDependency.RIGHT);
                    Log.d(TAG,"cur " + cur.getX() + " index " + (index));
                }
                else {
                    index = list.getEntryCount() - dataCountInPage;
                   if(index >= 0) {
                       MPPointF cur = getPosition(list.getEntryForIndex(index), YAxis.AxisDependency.RIGHT);
                       Log.d(TAG, "cur " + cur.getX() + " index " + (index));
                   }else{
                       index = 0;
                   }
                }
                if(onCharItemIndex != null){
                    onCharItemIndex.onItemIndex(index);
                }
            }
        }
    }
    public void initChartGestureListener()
    {
        OnTouchListener = getOnTouchListener();

        setOnChartGestureListener(new OnChartGestureListener() {
            @Override
            public void onChartGestureStart(MotionEvent motionEvent, ChartTouchListener.ChartGesture chartGesture) {

            }
            @Override
            public void onChartGestureEnd(MotionEvent motionEvent, ChartTouchListener.ChartGesture chartGesture) {
                computeIndex();
            }

            @Override
            public void onChartLongPressed(MotionEvent motionEvent) {

            }

            @Override
            public void onChartDoubleTapped(MotionEvent motionEvent) {

            }

            @Override
            public void onChartSingleTapped(MotionEvent motionEvent) {
                Log.d(TAG, "onChartSingleTapped: motionEvent "+motionEvent);
            }

            @Override
            public void onChartFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
                Log.d(TAG, "onChartFling: motionEvent "+motionEvent+" v "+v+" v1 "+v1);
            }

            @Override
            public void onChartScale(MotionEvent motionEvent, float v, float v1) {
                Log.d(TAG, "onChartScale: motionEvent "+motionEvent+" v "+v+" v1 "+v1);
            }

            @Override
            public void onChartTranslate(MotionEvent motionEvent, float v, float v1) {
                Log.d(TAG, "onChartTranslate: motionEvent "+motionEvent+" v "+v+" v1 "+v1);
                computeIndex();
            }
        });
        setOnTouchListener(new ChartTouchListener(this) {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return OnTouchListener.onTouch(v, event);
            }

            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                                   float velocityY) {
                return false;
            }

            public boolean onDoubleTap(MotionEvent e) {
                return OnTouchListener.onDoubleTap(e);
            }

            public void onLongPress(MotionEvent e) {
                OnTouchListener.onLongPress(e);
            }

            public boolean onSingleTapUp(MotionEvent e) {
                 return OnTouchListener.onSingleTapUp(e);
            }
        });

    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //重点在这里

        int action = ev.getAction();
//        Log.d("FlyBarChart","getScrollX " + ev.getX());
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                //不允许上层viewGroup拦截事件.
                getParent().requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_MOVE:
                //满足listView滑动到顶部，如果继续下滑，那就让scrollView拦截事件

                break;
            case MotionEvent.ACTION_UP:
                //不允许上层viewGroup拦截事件

                getParent().requestDisallowInterceptTouchEvent(false);

                break;
        }

        return super.dispatchTouchEvent(ev);
    }
}
